<template>
  <Modal v-model="comTransBalPop" width="32" :closable="false" :mask-closable="false"
    class-name="vertical-center-modal">
    <div slot="header" class="head-sty">
      <div class="title">Transfer To Balance</div>
      <Icon type="md-close" class="close" @click="closeMet()" />
    </div>
    <div class="content-sty">
      <div class="label-text">Transfer Amount（{{ userCurrency.code }}）</div>
      <Input v-model="form.transferAmount" placeholder="Please enter the amount you want to transfer" />
      <div class="tip-text">This operation cannot be undone. Please verify the amount carefully.</div>
    </div>
    <div slot="footer" class="footer-wrap">
      <div class="cancel-text" @click="closeMet()">Cancel</div>
      <div v-if="processedSettledBonusNum == 0" class="disable-con">Confirm</div>
      <div v-else class="confirm-text" @click="confirmBtn()">Confirm</div>
    </div>
  </Modal>
</template>
<script>
import { mapState } from "vuex";
export default {
  props: {
    settledBonusNum: {
      type: [Number, String],
      default: 0
    }
  },
  data() {
    return {
      comTransBalPop: false,
      form: {},
      errorMessage: ''
    }
  },
  computed: {
    ...mapState({
      userCurrency: state => state.user.userCurrency,
      userInfo: state => state.user.userInfo
    }),
    processedSettledBonusNum() {
      const value = parseFloat(this.settledBonusNum);
      return isNaN(value) ? 0 : value;
    }
  },
  methods: {
    open() {
      this.comTransBalPop = true;

    },
    closeMet() {
      this.comTransBalPop = false;
      this.clearInfo();
    },
    clearInfo() {
      delete this.form.transferAmount;
    },
    handleInput() {
      const inputValue = this.form.transferAmount;
      // 正则表达式匹配只包含数字和小数点，且小数点后最多两位
      const regex = /^[0-9]*(\.[0-9]{0,2})?$/;
      if (!regex.test(inputValue)) {
        this.$Message.warning('Please enter the specific amount, with a maximum of two decimal places.');
        // 去除不符合规则的字符
        this.form.transferAmount = inputValue.replace(/[^0-9.]/g, '');
        const parts = this.form.transferAmount.split('.');
        if (parts.length > 1) {
          parts[1] = parts[1].slice(0, 2);
          this.form.transferAmount = parts.join('.');
        }
      }
    },
    confirmBtn() {
      // 为空提示
      if (!this.form.transferAmount) {
        this.$Message.error('Please enter the amount you want to transfer!');
      }
      this.handleInput();//匹配输入正则
      // 不能大于settledBonusNum的值
      const numValue = parseFloat(this.form.transferAmount);
      if (!isNaN(numValue) && numValue > this.settledBonusNum) {
        this.$Message.error('The amount exceeds the transferable limit.');
        return;
      }

      this.$emit('transEmitInfo', this.form);
    }
  }
}
</script>

<style lang="less" scoped>
:deep(.ivu-modal-content) {
  border-radius: 12px;

  .ivu-modal-header {
    border: none;
    padding: 32px 32px;
  }

  .ivu-modal-body {
    padding: 8px 32px 6px 32px;
  }

  .ivu-modal-footer {
    border: none;
    padding: 32px;
  }
}

.vertical-center-modal {
  display: flex;
  align-items: center;
  justify-content: center;

  .head-sty {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .title {
      font-weight: 600;
      font-size: 24px;
      color: #1E1714;
      line-height: 28px;
    }

    .close {
      color: #1E1714;
      font-size: 36px;
      font-weight: 500;
      cursor: pointer;
    }
  }

  .content-sty {
    .label-text {
      font-weight: 600;
      font-size: 20px;
      color: #1E1714;
      line-height: 23px;
      margin-bottom: 20px;
    }

    :deep(.ivu-input) {
      height: 54px;
      padding: 16px;
      border-radius: 4px;
      font-weight: 500;
      font-size: 16px;
      line-height: 19px;
    }

    .tip-text {
      font-weight: 500;
      font-size: 16px;
      color: #FF186B;
      line-height: 19px;
      margin-top: 12px;
    }
  }

  .footer-wrap {
    display: flex;
    justify-content: end;
    gap: 24px;

    .cancel-text {
      height: 46px;
      padding: 12px 30px;
      border-radius: 8px 8px 8px 8px;
      border: 1px solid #E5E5E5;
      font-family: Open Sans, Open Sans;
      font-weight: 400;
      font-size: 16px;
      color: #1E1714;
      line-height: 19px;
      cursor: pointer;
    }

    .confirm-text {
      height: 46px;
      padding: 12px 30px;
      background: #F21473;
      border-radius: 8px 8px 8px 8px;
      font-family: Open Sans, Open Sans;
      font-weight: 600;
      font-size: 16px;
      color: #FFFFFF;
      line-height: 19px;
      cursor: pointer;
    }

    .disable-con {
      height: 46px;
      padding: 12px 30px;
      background: #AEB0B9;
      border-radius: 8px 8px 8px 8px;
      font-family: Open Sans, Open Sans;
      font-weight: 600;
      font-size: 16px;
      color: #FFFFFF;
      line-height: 19px;
      cursor: not-allowed;
    }
  }
}
</style>
